//
//  BVSportsSliderView.m
//  duobao
//
//  Created by YYKJ on 2017/7/8.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVSportsSliderView.h"

#define titleHeight 40

@implementation BVSportsSliderView

-(id)initWithFrame:(CGRect)frame titleArr:(NSArray *)arr;{
    
    if (self = [super initWithFrame:frame]) {
        self.titleArray = arr;
        [self layoutUI];
    }
    return self;
}

-(void)layoutUI{
    
    UIView *topLine = [[UIView alloc]init];
    [self addSubview:topLine];
    [topLine setBackgroundColor:[UIColor BVLineGrayColor]];
    [topLine setFrame:CGRectMake(0, 0, ScreenW, 0.5)];
    
    CGFloat titleWidth = self.size.width/self.titleArray.count;
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    _buttonArray = [NSMutableArray array];
    
    for (int i = 0; i < self.titleArray.count; i++) {
        
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:titleButton];
        titleButton.frame = CGRectMake(titleWidth*i, 1, titleWidth, 40);
        [titleButton setTitle:[NSString stringWithFormat:@"%@",self.titleArray[i]] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor BVTextGrayColor] forState:UIControlStateNormal];
        [titleButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [titleButton addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTag:i];
        
        if (i == 0) {
            self.selectButton = titleButton;
            [self.selectButton setTitleColor:[UIColor BVBaseOrangeColor] forState:UIControlStateNormal];
        }
        [_buttonArray addObject:titleButton];
    }
    
    for (int i = 0; i < self.titleArray.count; i++) {
        UIView *lin = [[UIView alloc]init];
        [self addSubview:lin];
        [lin setBackgroundColor:[UIColor BVLineGrayColor]];
        [lin setFrame:CGRectMake(titleWidth*(i+1), 5, 1, 30)];
        [lin setHidden:YES];
    }
    
    UIView *linTwo = [[UIView alloc]init];
    [self addSubview:linTwo];
    [linTwo setBackgroundColor:[UIColor BVLineGrayColor]];
    [linTwo setFrame:CGRectMake(0, 39, ScreenW, 0.5)];
    
    self.sliderView = [[UIView alloc]initWithFrame:CGRectMake(30, titleHeight-1, titleWidth-60, 1)];
    self.sliderView.backgroundColor = [UIColor BVBaseOrangeColor];
    [self addSubview:self.sliderView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youxi:) name:@"1234" object:nil];
    
}

- (void)scrollViewSelectToIndex:(UIButton *)button{
    
    [self selectButton:button.tag];
}

- (void)selectButton:(NSInteger)index{
    
    [self.selectButton setTitleColor:[UIColor BVTextGrayColor] forState:UIControlStateNormal];
    self.selectButton = _buttonArray[index];
    [self.selectButton setTitleColor:[UIColor BVBaseOrangeColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.2 animations:^{
        self.sliderView.frame = CGRectMake((self.width/self.titleArray.count*index)+30, titleHeight-1, (self.width/self.titleArray.count)-60, 1);
        
    }];
    
    if (self.btnClick) {
        self.btnClick(index);
    }
}

-(void)youxi:(NSNotification *)send{
    
    [self selectButton:[send.userInfo[@"index"] intValue]];
    
}

@end
